$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'notice/list',
        datatype: "json",
        colModel: [
            { label: '主键', name: 'id', index: "id", width: 110, hidden:true, key: true },
            { label: '标题', name: 'noticeTitle', width: 45 },
            { label: '是否发布', name: 'noticeStatusName', width: 90 },
            { label: '生成时间', name: 'createTime', index: "create_time", width: 85}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.result",
            page: "page.pageNo",
            total: "page.totalPages",
            records: "page.total"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            noticeTitle: null
        },
        showList:true,
        title:null,
        editor: false,
        notice: {
            pushFlag:false
            /*noticeTitle:null,
            noticeContent:null*/
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.editor = true;
            editor.txt.html('');
            vm.notice = {pushFlag:false,noticeTitle:null,noticeContent:null}
        },
        update: function(){
            var grid = $("#jqGrid");
            var noticeId = grid.getGridParam("selrow");
            if(!noticeId){
                alert("请选择一条记录");
                return ;
            }
            $.get(baseURL + "notice/info/"+noticeId, function(r){
                if (r.code === "success"){
                    vm.notice = r.data;
                    editor.txt.html(r.data.noticeContent);
                    vm.showList = false;
                    vm.title = "修改";
                    vm.editor = true;
                } else {
                    alert(r.message)
                }
            });
            //vm.getNotice(noticeId);
        },
        getNotice: function(noticeId){
            $.get(baseURL + "notice/info/"+noticeId, function(r){
                if (r.code.equals("success")){
                    vm.notice = r.notice;
                    editor.txt.html(r.notice.noticeContent);
                } else {
                    alert(r.message)
                }
            });
        },
        push: function(){
            vm.notice.pushFlag = true;
            vm.saveOrUpdate();
        },
        saveOrUpdate: function(){
            if (!vm.validate()){
                return
            }
            var url = "notice/saveOrUpdate";
            vm.notice.noticeContent = editor.txt.html();
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.notice),
                success: function(r){
                    if(r.code === 'success'){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.message);
                    }
                }
            });
        },
        validate:function () {
            if (isBlank(vm.notice.noticeTitle)){
                showTips("标题不能为空", '#noticeTitle');
                return false;
            }
            if (getByteLen(vm.notice.noticeTitle)>40){
                showTips("标题不能超过20个字", '#noticeTitle');
                return false;
            }
            return true;
        },
        reload: function () {
            vm.showList = true;
            vm.editor = false;
            var page = $("#jqGrid").jqGrid("getGridParam", page);
            $("#jqGrid").jqGrid('setGridParam',{
                postData: {"noticeTitle":vm.q.noticeTitle},
                page:page
            }).trigger("reloadGrid");
        }
    }
});

/*富文本编辑*/
var E = window.wangEditor;
var editor = new E('#editor1');
editor.customConfig.uploadImgServer = '/upload/image';
editor.customConfig.uploadFileName = 'files';

// 监听编辑区域，替换图片URL
editor.customConfig.onchange = function (html) {
    var imgs = document.getElementById(editor.textElemId).getElementsByTagName('img');
    if (imgs.length>0){
        for(i=0;i<imgs.length;i++){
            var imgUrl = imgs[i].src;
            if (!urlRegEx(imgUrl)){
                $.ajax({
                    type: "GET",
                    url: baseURL + "upload/uploadByUrl/image",
                    contentType: "application/json",
                    async : false,
                    data: {url:imgUrl},
                    success: function(r){
                        if(r.code === 'success'){
                            imgs[i].src = r.data.fileUrl;
                        }
                    }
                });
            }
        }
    }
};
editor.create();

// 截取图片URL校验是否为自己文件服务器地址
function urlRegEx(url) {
    //var url = "http://img.qiniu.vp359.com/0092746c8ecf45589b7127f9526700f9.png";
    //var re = /(\w+):\/\/([^\:|\/]+)(\:\d*)?(.*\/)([^#|\?|\n]+)?(#.*)?(\?.*)?/i;
    var ree = /http[s]?:\/\/(.*?)([:\/]|$)/;
    var arr = url.match(ree);
    console.info(arr[1]);
    if (arr[1]=="img.qiniu.vp359.com"){
        return true;
    }
    return false;
}

